package com.zhixiang.core.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhixiang.xaec.service.system.SystemLogService;

/**
 * 
 * 系统日志的切面类
 * @author Hello World
 *
 */
@Aspect
@Component
public class LogAspect
{   
    //记录是添加还是修改
    String opration="";
    
    //如果是修改，则用来保存修改前的对象
    Object befoModifOrDele;
    
    @Autowired
    SystemLogService sysLogService;
    
    //dao添加或者修改方法切点
    @Pointcut("execution(* com.zhixiang.core.dao.impl.BaseDaoImpl.save(..))")
    public void  saveCall(){} 
    
    //dao删除方法切点
    @Pointcut("execution(* com.zhixiang.core.dao.impl.BaseDaoImpl.remove(..))")
    public void  removeCall(){} 

    //定义通知（前置通知）作用：判断操作为添加还是修改
    @Before(value="saveCall()")
    public void beforeSaveCallCalls(JoinPoint joinPoint) throws Throwable{      
        opration=sysLogService.judgeOpration(joinPoint);
        if("修改".equals(opration)){
            befoModifOrDele=sysLogService.befoModifOrDele(joinPoint);
        }else{
            befoModifOrDele=null;
        }
    }
    
    //定义通知（后置通知）
    @AfterReturning(value="saveCall()")  
    public void afterSaveCallCalls(JoinPoint joinPoint) throws Throwable{
        //调用此方法传递三个参数：切点、操作、修改前的对象
        sysLogService.insertLog(joinPoint,opration,befoModifOrDele);       
    }
    
    //定义通知（环绕通知）
    //环绕通知便于删除前先查到要删除的对象信息
    @Around(value="removeCall()")  
    public void removeCallCalls(ProceedingJoinPoint  joinPoint) throws Throwable{
        befoModifOrDele=sysLogService.befoModifOrDele(joinPoint);
        joinPoint.proceed();
        sysLogService.insertLog(joinPoint,"删除",befoModifOrDele); 
        
    }
}
